package gcontent.utils;

import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

public class URLUtils {

	
	public static String getURLHost(URL url)
	{
		String ret;
		
		if(url.getPort() == -1)
			ret = url.getProtocol() + "://" + url.getHost();
		else
			ret = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();
		
		return ret;
	}

	public static String getURLDir(URL url)
	{
		String ret;
	
		ret = url.getPath();
		if(ret == null || ret.length() == 0)
			ret = "/";			
			
		return ret;
	}
	
	public static String getURLHost(String url_path)
	{
		String ret_tok;
		int ret;
		
		ret = url_path.indexOf("://");
		if(ret < 0)
			return null;
		
		if((ret = url_path.indexOf("/", ret + 3)) > 0)
			ret_tok = url_path.substring(0, ret);
		else
			ret_tok = url_path;
		
		return ret_tok;
	}
	
	public static String getURLURI(String url_path)
	{
		String ret_tok;
		int ret;
		
		ret = url_path.indexOf("://");
		if(ret < 0)
			return null;
		
		if((ret = url_path.indexOf("/", ret + 3)) > 0)
			ret_tok = url_path.substring(ret, url_path.length());
		else
			ret_tok = "/";
		
		return ret_tok;
	}
	
	public static String getURIDirectory(String url_path)
	{
		int ret, last_idx;
		
		ret = url_path.indexOf("://");
		if(ret < 0)
			return null;
		
		last_idx = -1;
		for(int i = ret + 3; i < url_path.length(); i++)
			if(url_path.charAt(i) == '/')
				last_idx = i;
		
		if(last_idx > -1) 
			return url_path.substring(0, last_idx);
		
		return url_path;
	}
	
	public static String getDirectory(String dir_path)
	{		
		int ret_idx;
		
		if(dir_path == null || dir_path.length() == 0)
			return "/";

		ret_idx = dir_path.lastIndexOf('/');
		if(ret_idx == -1)
			return "/";
		
		return dir_path.substring(0, ret_idx + 1);
	}
	
	public static String getDomainToken(String hostname, int subdomain_index)
	{
		int dots, i;
		
		dots = 0;
		for(i = hostname.length() - 1; i >= 0; i--) {
			if(hostname.charAt(i) == '.') {
				if(subdomain_index == dots)
					break;
				
				dots++;
			}
		}
		
		if(i == -1)
			return hostname;
		
		if(hostname.charAt(i) == '.')
			i++;
		
		return hostname.substring(i, hostname.length());		
	}

	
	public static Vector<String> getHeaderValues(URLConnection con, String key_name)
	{
		String name;
		Vector<String> values;
		
		values = new Vector<String>();
	    for (int i = 1; (name = con.getHeaderFieldKey(i)) != null; i++) {
	        if (name.equalsIgnoreCase(key_name)) {
	        	values.add(con.getHeaderField(i));
	        }
		}
	    
	    return values;
	}

	public static String replaceProtocol(String url, String orig, String dest)
	{
		return url.replace(orig, dest);
	}
	
	public static String getURLProtocol(String url_path)
	{
		String protocol = null;
		int idx;
		
		idx = url_path.indexOf(":");
		if (idx >= 0)
			protocol = new String(url_path.substring(0, idx)).toUpperCase();
		
		return protocol;
	}
	
	public static String splitWorldWideWeb(String url_path)
	{
		return url_path.replace("www.", "");
	}
	
	public static String splitProtocol(String url_path)
	{
		String url = "";
		int idx;
		
		idx = url_path.indexOf("://");
		if (idx >= 0)
			url = new String(url_path.substring(idx + 3));
		else {
			idx = url_path.indexOf(":/");
			if (idx >= 0)
				url = new String(url_path.substring(idx + 2));
		}			
		
		return url;
	}
	
	public static String normalizeDNSIp(String ip)
	{
		if (ip.endsWith("."))
			ip = ip.substring(0, ip.length() - 1);
		
		return ip;
	}
}

